#include <cstdio>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <errno.h>
using namespace std;

void SetNonBlock(int fd)
{
    int fl = fcntl(fd,F_GETFL);
    if(fl < 0)
    {
        perror("fcntl");
        return;
    }
    fcntl(fd,F_SETFL,fl|O_NONBLOCK);
}

int main()
{
    char buffer[1024];
    SetNonBlock(0);
    while(true)
    {   
        // printf("Please Enter: ");
        // fflush(stdout);
        ssize_t n = read(0,buffer,sizeof(buffer));
        if(n > 0)
        {
            buffer[n-1] = 0;
            cout << "echo: "<<buffer <<endl;
        }
        else if(n == 0)
        {
            cout << "read done"<<endl;
            break;
        }
        else
        {
            if(errno == EWOULDBLOCK)
            {
                cout<<"0 fd data not ready, try again"<<endl;
                sleep(1);
            }
            else{
            cerr << "read error"<<endl;
            break;
            }
        }
    }

    return 0;
}